package org.liukai.ssh_ems.interceptor;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;
import org.liukai.ssh_ems.entity.po.User;

import java.util.Map;

/**
 * @project ssh_ems
 * @create LoginInterceptor.java 
 * 
 * @author liukai
 * @date 2016/1/14 16:13
 * @detail 
 */
public class LoginInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        // 取得请求相关的ActionContext实例
        ActionContext ctx = actionInvocation.getInvocationContext();
        String urlString = ServletActionContext.getRequest().getRequestURI();
        //排除例外
        if(urlString.endsWith("/user/toLogin") || urlString.endsWith("/user/login")) {
            return actionInvocation.invoke();
        }
        //查看当前用户中有没有currentUser
        Map session = ctx.getSession();
        User user = (User) session.get("currentUser");
        //有就继续执行别的拦截器
        if (user != null) {
            return actionInvocation.invoke();
        }
        //没有就跳转到登录页面
        Map<String, Object> request = (Map<String, Object>) ctx.get("request");
        request.put("msg", "请先登录");
        return Action.LOGIN;
    }
}
